package com.jeeplus.modules.kpi.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.model.ResultModel;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.process.AchievementEnum;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.action.entity.UserAction;
import com.jeeplus.modules.action.service.UserActionService;
import com.jeeplus.modules.kpi.entity.AchievementInfo;
import com.jeeplus.modules.kpi.service.AchievementInfoService;
import com.jeeplus.modules.process.entity.ProcessInfo;
import com.jeeplus.modules.process.service.ProcessInfoService;
import com.jeeplus.modules.sys.echartDto.ResultVo;
import com.jeeplus.modules.sys.entity.Result;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;

/**
 * 绩效考核流程Controller
 * @author wangkai
 * @version 2017-12-25
 */
@Controller
@RequestMapping(value = "${adminPath}/kpi/achievementInfo")
public class AchievementInfoController extends BaseController {

	@Autowired
	private AchievementInfoService achievementInfoService;
	
	@Autowired
	private UserActionService userActionService;
	
	@Autowired
	private ProcessInfoService processInfoService;
	@ModelAttribute
	public AchievementInfo get(@RequestParam(required=false) String id) {
		AchievementInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = achievementInfoService.get(id);
		}
		if (entity == null){
			entity = new AchievementInfo();
		}
		return entity;
	}
	
	/**
	 * 绩效考核流程列表页面
	 */
	@RequiresPermissions("kpi:achievementInfo:list")
	@RequestMapping(value = {"list", ""})
	public String list(AchievementInfo achievementInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		achievementInfo.setCreateBy(UserUtils.getUser());
		Page<AchievementInfo> page = achievementInfoService.findPage(new Page<AchievementInfo>(request, response), achievementInfo); 
		model.addAttribute("page", page);
		return "modules/kpi/achievementInfoList";
	}

	/**
	 * 查看，增加，编辑绩效考核流程表单页面
	 */
	@RequiresPermissions(value={"kpi:achievementInfo:view","kpi:achievementInfo:add","kpi:achievementInfo:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(AchievementInfo achievementInfo, Model model) {
		model.addAttribute("achievementInfo", achievementInfo);
		return "modules/kpi/achievementInfoForm";
	}
	
	@RequestMapping(value = "see")
	public String see(AchievementInfo achievementInfo, Model model) {
		ProcessInfo processInfo = new ProcessInfo();
		processInfo.setProcessId(achievementInfo.getId());
		List<ProcessInfo> pList = processInfoService.findList(processInfo);
		
		String officeId = UserUtils.getUser().getOffice().getId();
		List<String> roleList = UserUtils.getUser().getRoleIdList();
		String pNode = null;
		if (roleList.contains("671a5d428d4b4dfb9a5a7899546ef5bc")) { // 部门领导
			if (officeId.equals("e8e3d9509b4141269e352720f4b15048")) { // 科研处
				pNode = String.valueOf(AchievementEnum.SCIENTIFIC.getProcess());
			} else if (officeId.equals("fc2f1474f9ba479ba06096cd0a92c9ae")) { // 政工处
				pNode = String.valueOf(AchievementEnum.POLITICAL .getProcess());
			} else {
				pNode = String.valueOf(AchievementEnum.DEPARTMENT.getProcess());
			}	
		}
		
		model.addAttribute("pNode", pNode);
		model.addAttribute("processInfo", pList);
		model.addAttribute("achievementInfo", achievementInfo);
		return "modules/kpi/achievementInfoSee";
	}
	
	@RequestMapping(value = "examine", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel examine(HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		String aId = request.getParameter("aId"); // 流程id
		String isNext = request.getParameter("isNext"); // 是否同意
		String opinion = request.getParameter("opinion"); // 审核意见
		String node = request.getParameter("node"); // 节点
		
		if (StringUtils.isEmpty(aId) || StringUtils.isEmpty(isNext) || StringUtils.isEmpty(opinion) || StringUtils.isEmpty(node)) {
			return ResultModel.failResult("缺少参数");
		}
		
		AchievementInfo achievementInfo = new AchievementInfo();
		achievementInfo.setId(aId);
		
		ProcessInfo processInfo = new ProcessInfo();
		processInfo.setProcessId(aId);
		
		if (Integer.parseInt(node) == AchievementEnum.DEPARTMENT.getProcess()) { // 部门审核
			achievementInfo.setOpinionDepartment(opinion);
			achievementInfo.setOpinionDepartmentDate(new Date());
			achievementInfo.setOpinionDepartmentSignature(user.getName());
			if (Integer.parseInt(isNext) == 2) { // 不同意
				achievementInfo.setProcessNode(String.valueOf(AchievementEnum.END.getProcess()));
				processInfo.setProcessIndex(AchievementEnum.END.getProcess());
				processInfo.setProcessText("部门审核未通过，流程结束。审核意见："+opinion);
			} else {
				achievementInfo.setProcessNode(String.valueOf(AchievementEnum.SCIENTIFIC.getProcess()));
				processInfo.setProcessIndex(AchievementEnum.SCIENTIFIC.getProcess());
				processInfo.setProcessText("部门审核通过，等待科研处审核。审核意见："+opinion);
			}
			achievementInfoService.updateDepartmentExamine(achievementInfo);
		} else if (Integer.parseInt(node) == AchievementEnum.SCIENTIFIC.getProcess()) { // 科研处审核
			achievementInfo.setOpinionScientific(opinion);
			achievementInfo.setOpinionScientificDate(new Date());
			achievementInfo.setOpinionScientificSignature(user.getName());
			if (Integer.parseInt(isNext) == 2) { // 不同意
				achievementInfo.setProcessNode(String.valueOf(AchievementEnum.DEPARTMENT.getProcess()));
				processInfo.setProcessIndex(AchievementEnum.DEPARTMENT.getProcess());
				processInfo.setProcessText("科研处审核未通过，退回部门审核。审核意见："+opinion);
			} else {
				achievementInfo.setProcessNode(String.valueOf(AchievementEnum.POLITICAL.getProcess()));
				processInfo.setProcessIndex(AchievementEnum.POLITICAL.getProcess());
				processInfo.setProcessText("科研处审核通过，等待政工处审核。审核意见："+opinion);
			}
			achievementInfoService.updateScientificExamine(achievementInfo);
		} else if (Integer.parseInt(node) == AchievementEnum.POLITICAL.getProcess()) { // 政工处审核
			achievementInfo.setOpinionPolitical(opinion);
			achievementInfo.setOpinionPoliticalDate(new Date());
			achievementInfo.setOpinionPoliticalSignature(user.getName());
			if (Integer.parseInt(isNext) == 2) { // 不同意
				achievementInfo.setProcessNode(String.valueOf(AchievementEnum.DEPARTMENT.getProcess()));
				processInfo.setProcessIndex(AchievementEnum.DEPARTMENT.getProcess());
				processInfo.setProcessText("政工处审核未通过，退回部门审核。审核意见："+opinion);
			} else {
				achievementInfo.setProcessNode(String.valueOf(AchievementEnum.END.getProcess()));
				processInfo.setProcessIndex(AchievementEnum.END.getProcess());
				processInfo.setProcessText("政工处审核通过，流程结束。审核意见："+opinion);
			}
			achievementInfoService.updatePoliticalExamine(achievementInfo);
		}
		achievementInfoService.updateProcessNode(achievementInfo);
		processInfoService.save(processInfo);
		
		return ResultModel.succResult();
	}
	
	/**
	 * 流程申请表单
	 */
	@RequiresPermissions(value={"kpi:achievementInfo:add"})
	@RequestMapping(value = "add")
	public String add(AchievementInfo achievementInfo, Model model) {
		Calendar rightNow = Calendar.getInstance();
		Date endTime = rightNow.getTime();
		rightNow.add(Calendar.YEAR, -1);
		Date startTime = rightNow.getTime();
		model.addAttribute("startTime",startTime);
		model.addAttribute("endTime", endTime);
		return "modules/kpi/achievementInfoAdd";
	}
	
	/**
	 * 保存绩效考核流程
	 */
	@RequiresPermissions(value={"kpi:achievementInfo:add"})
	@RequestMapping(value = "addFlow")
	public String addFlow(AchievementInfo achievementInfo, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (!beanValidator(model, achievementInfo)){
			return form(achievementInfo, model);
		}
		Double total = 0.0;
		if (achievementInfo.getOutsideNationalLevel() != null) {
			total += achievementInfo.getOutsideNationalLevel();
		}
		if (achievementInfo.getOutsideAutonomousRegionLevel() != null) {
			total += achievementInfo.getOutsideAutonomousRegionLevel();
		}
		if (achievementInfo.getOutsideCityLevel() != null) {
			total += achievementInfo.getOutsideCityLevel();
		}
		if (achievementInfo.getOutsideOtherLevel() != null) {
			total += achievementInfo.getOutsideOtherLevel();
		}
		if (achievementInfo.getInsideGreatTask() != null) {
			total += achievementInfo.getInsideGreatTask();
		}
		if (achievementInfo.getInsideImportance_task() != null) {
			total += achievementInfo.getInsideImportance_task();
		}
		if (achievementInfo.getInsideBoutique_task() != null) {
			total += achievementInfo.getInsideBoutique_task();
		}
		if (achievementInfo.getInsideGeneral_task() != null) {
			total += achievementInfo.getInsideGeneral_task();
		}
		if (achievementInfo.getInsideYouthTask() != null) {
			total += achievementInfo.getInsideYouthTask();
		}
		if (achievementInfo.getWorkMonograph() != null) {
			total += achievementInfo.getWorkMonograph();
		}
		if (achievementInfo.getWorkTranslation() != null) {
			total += achievementInfo.getWorkTranslation();
		}
		if (achievementInfo.getWorkWrite() != null) {
			total += achievementInfo.getWorkWrite();
		}
		if (achievementInfo.getWorkOther() != null) {
			total += achievementInfo.getWorkOther();
		}
		if (achievementInfo.getThesisForeign() != null) {
			total += achievementInfo.getThesisForeign();
		}
		if (achievementInfo.getThesisNewspaper() != null) {
			total += achievementInfo.getThesisNewspaper();
		}
		if (achievementInfo.getThesisNational() != null) {
			total += achievementInfo.getThesisNational();
		}
		if (achievementInfo.getThesisInside() != null) {
			total += achievementInfo.getThesisInside();
		}
		if (achievementInfo.getThesisGeneral() != null) {
			total += achievementInfo.getThesisGeneral();
		}
		if (achievementInfo.getReprintAllXinhua() != null) {
			total += achievementInfo.getReprintAllXinhua();
		}
		if (achievementInfo.getReprintAllRenda() != null) {
			total += achievementInfo.getReprintAllRenda();
		}
		if (achievementInfo.getReprintPartXinhua() != null) {
			total += achievementInfo.getReprintPartXinhua();
		}
		if (achievementInfo.getReprintPartRenda() != null) {
			total += achievementInfo.getReprintPartRenda();
		}
		if (achievementInfo.getInstructionsCommittee() != null) {
			total += achievementInfo.getInstructionsCommittee();
		}
		if (achievementInfo.getInstructionsCommitteeM() != null) {
			total += achievementInfo.getInstructionsCommitteeM();
		}
		if (achievementInfo.getInstructionsAutonomous() != null) {
			total += achievementInfo.getInstructionsAutonomous();
		}
		if (achievementInfo.getAwardsNational() != null) {
			total += achievementInfo.getAwardsNational();
		}
		if (achievementInfo.getAwardsAutonomous() != null) {
			total += achievementInfo.getAwardsAutonomous();
		}
		if (achievementInfo.getAwardsCity() != null) {
			total += achievementInfo.getAwardsCity();
		}
		if (achievementInfo.getAwardsOther() != null) {
			total += achievementInfo.getAwardsOther();
		}
		if (achievementInfo.getActivityNational() != null) {
			total += achievementInfo.getActivityNational();
		}
		if (achievementInfo.getActivityAutonomous() != null) {
			total += achievementInfo.getActivityAutonomous();
		}
		if (achievementInfo.getActivityCity() != null) {
			total += achievementInfo.getActivityCity();
		}
		if (achievementInfo.getActivityOther() != null) {
			total += achievementInfo.getActivityOther();
		}
		if (achievementInfo.getTaskAutonomousDo() != null) {
			total += achievementInfo.getTaskAutonomousDo();
		}
		if (achievementInfo.getTaskAutonomousGet() != null) {
			total += achievementInfo.getTaskAutonomousGet();
		}
		if (achievementInfo.getTaskInsideDo() != null) {
			total += achievementInfo.getTaskInsideDo();
		}
		if (achievementInfo.getTaskInsideGet() != null) {
			total += achievementInfo.getTaskInsideGet();
		}
		if (achievementInfo.getPublishManuscript() != null) {
			total += achievementInfo.getPublishManuscript();
		}
		if (achievementInfo.getPublishFirstTrial() != null) {
			total += achievementInfo.getPublishFirstTrial();
		}
		if (achievementInfo.getPublishRecheck() != null) {
			total += achievementInfo.getPublishRecheck();
		}
		if (achievementInfo.getPublishLastInstance() != null) {
			total += achievementInfo.getPublishLastInstance();
		}
		if (achievementInfo.getPublishCompiling() != null) {
			total += achievementInfo.getPublishCompiling();
		}
		if (achievementInfo.getPublishEdit() != null) {
			total += achievementInfo.getPublishEdit();
		}
		if (achievementInfo.getPublishProofread() != null) {
			total += achievementInfo.getPublishProofread();
		}
		if (achievementInfo.getPublishDesign() != null) {
			total += achievementInfo.getPublishDesign();
		}
		if (achievementInfo.getPublishInput() != null) {
			total += achievementInfo.getPublishInput();
		}
		if (achievementInfo.getPublishWrote() != null) {
			total += achievementInfo.getPublishWrote();
		}
		if (achievementInfo.getPublishTotal() != null) {
			total += achievementInfo.getPublishTotal();
		}
		if (achievementInfo.getPublishReport() != null) {
			total += achievementInfo.getPublishReport();
		}
		if (achievementInfo.getPublishCommunication() != null) {
			total += achievementInfo.getPublishCommunication();
		}
		if (achievementInfo.getBooksBorrow() != null) {
			total += achievementInfo.getBooksBorrow();
		}
		if (achievementInfo.getBooksPublish() != null) {
			total += achievementInfo.getBooksPublish();
		}
		if (achievementInfo.getBooksPurchase() != null) {
			total += achievementInfo.getBooksPurchase();
		}
		if (achievementInfo.getBooksTotal() != null) {
			total += achievementInfo.getBooksTotal();
		}
		if (achievementInfo.getBooksConsult() != null) {
			total += achievementInfo.getBooksConsult();
		}
		if (achievementInfo.getNetworkReport() != null) {
			total += achievementInfo.getNetworkReport();
		}
		if (achievementInfo.getNetworkVisit() != null) {
			total += achievementInfo.getNetworkVisit();
		}
		if (achievementInfo.getNetworkMaintain() != null) {
			total += achievementInfo.getNetworkMaintain();
		}
		if (achievementInfo.getNetworkConsult() != null) {
			total += achievementInfo.getNetworkConsult();
		}
		if (achievementInfo.getNetworkTotal() != null) {
			total += achievementInfo.getNetworkTotal();
		}
		if (achievementInfo.getSynthesizeSeniorsPolitical() != null) {
			total += achievementInfo.getSynthesizeSeniorsPolitical();
		}
		if (achievementInfo.getSynthesizeSeniorsScientific() != null) {
			total += achievementInfo.getSynthesizeSeniorsScientific();
		}
		if (achievementInfo.getSynthesizeSeniorsDepartment() != null) {
			total += achievementInfo.getSynthesizeSeniorsDepartment();
		}
		if (achievementInfo.getSynthesizeAdvancedScientific() != null) {
			total += achievementInfo.getSynthesizeAdvancedScientific();
		}
		if (achievementInfo.getSynthesizeAdvancedDepartment() != null) {
			total += achievementInfo.getSynthesizeAdvancedDepartment();
		}
		if (achievementInfo.getSynthesizeMedium() != null) {
			total += achievementInfo.getSynthesizeMedium();
		}
		if (achievementInfo.getSynthesizeOther() != null) {
			total += achievementInfo.getSynthesizeOther();
		}
		
		boolean edit = false;
		
		if(!achievementInfo.getIsNewRecord()){//编辑
			AchievementInfo t = achievementInfoService.get(achievementInfo.getId());//从数据库取出记录的值
			achievementInfo.setTotalPoints(total);
			MyBeanUtils.copyBeanNotNull2Bean(achievementInfo, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			achievementInfoService.save(t);//保存
			edit = true;
		}else{//新增
			achievementInfo.setTotalPoints(total);
			achievementInfo.setProcessNode(String.valueOf(AchievementEnum.DEPARTMENT.getProcess()));
			achievementInfo.setOfficeId(UserUtils.getUser().getOffice().getId());
			achievementInfo.setOpinionSignature(UserUtils.getUser().getName());
			achievementInfo.setOpinionDate(new Date());
			achievementInfoService.insertProcess(achievementInfo);//保存
			
			// 保存流程节点
			String id = achievementInfo.getId();
			if (!StringUtils.isEmpty(id)) {
				ProcessInfo processInfo = new ProcessInfo();
				processInfo.setProcessId(id);
				processInfo.setProcessIndex(AchievementEnum.DEPARTMENT.getProcess());
				processInfo.setProcessText("等待部门审核");
				processInfoService.save(processInfo);
			}
		}
		
		UserAction userAction = new UserAction();
		userAction.setAction("发起绩效考核流程");
		userActionService.save(userAction);
		
		addMessage(redirectAttributes, "提交绩效考核流程成功");
		
		if (edit) {
			return examineList(achievementInfo, request, response, model);
		} else {
			achievementInfo.setCreateBy(UserUtils.getUser());
			Page<AchievementInfo> page = achievementInfoService.findPage(new Page<AchievementInfo>(request, response), achievementInfo); 
			model.addAttribute("page", page);
			return "modules/kpi/achievementInfoList";	
		}
		
	}

	/**
	 * 保存绩效考核流程
	 */
	@RequiresPermissions(value={"kpi:achievementInfo:add","kpi:achievementInfo:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(AchievementInfo achievementInfo, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, achievementInfo)){
			return form(achievementInfo, model);
		}
		if(!achievementInfo.getIsNewRecord()){//编辑表单保存
			AchievementInfo t = achievementInfoService.get(achievementInfo.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(achievementInfo, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			achievementInfoService.save(t);//保存
		}else{//新增表单保存
			achievementInfoService.save(achievementInfo);//保存
		}
		addMessage(redirectAttributes, "保存绩效考核流程成功");
		return "redirect:"+Global.getAdminPath()+"/kpi/achievementInfo/?repage";
	}
	
	/**
	 * 删除绩效考核流程
	 */
	@RequiresPermissions("kpi:achievementInfo:del")
	@RequestMapping(value = "delete")
	public String delete(AchievementInfo achievementInfo, RedirectAttributes redirectAttributes) {
		achievementInfoService.delete(achievementInfo);
		addMessage(redirectAttributes, "删除绩效考核流程成功");
		return "redirect:"+Global.getAdminPath()+"/kpi/achievementInfo/?repage";
	}
	
	/**
	 * 批量删除绩效考核流程
	 */
	@RequiresPermissions("kpi:achievementInfo:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			achievementInfoService.delete(achievementInfoService.get(id));
		}
		addMessage(redirectAttributes, "删除绩效考核流程成功");
		return "redirect:"+Global.getAdminPath()+"/kpi/achievementInfo/?repage";
	}
	
	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("kpi:achievementInfo:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(AchievementInfo achievementInfo, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "绩效考核流程"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<AchievementInfo> page = achievementInfoService.findPage(new Page<AchievementInfo>(request, response, -1), achievementInfo);
    		new ExportExcel("绩效考核流程", AchievementInfo.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出绩效考核流程记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/kpi/achievementInfo/?repage";
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("kpi:achievementInfo:import")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<AchievementInfo> list = ei.getDataList(AchievementInfo.class);
			for (AchievementInfo achievementInfo : list){
				try{
					achievementInfoService.save(achievementInfo);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条绩效考核流程记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条绩效考核流程记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入绩效考核流程失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/kpi/achievementInfo/?repage";
    }
	
	/**
	 * 下载导入绩效考核流程数据模板
	 */
	@RequiresPermissions("kpi:achievementInfo:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "绩效考核流程数据导入模板.xlsx";
    		List<AchievementInfo> list = Lists.newArrayList(); 
    		new ExportExcel("绩效考核流程数据", AchievementInfo.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/kpi/achievementInfo/?repage";
    }
	
	@RequestMapping(value = "examineList")
	public String examineList(AchievementInfo achievementInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		String officeId = UserUtils.getUser().getOffice().getId();
		List<String> roleList = UserUtils.getUser().getRoleIdList();
		Page<AchievementInfo> page = null;
		String pNode = null;
		if (roleList.contains("671a5d428d4b4dfb9a5a7899546ef5bc")) { // 部门领导
			if (officeId.equals("e8e3d9509b4141269e352720f4b15048")) { // 科研处
				pNode = String.valueOf(AchievementEnum.SCIENTIFIC.getProcess());
				achievementInfo.setProcessNode(pNode);
				page = achievementInfoService.findPage(new Page<AchievementInfo>(request, response), achievementInfo); 
			} else if (officeId.equals("fc2f1474f9ba479ba06096cd0a92c9ae")) { // 政工处
				pNode = String.valueOf(AchievementEnum.POLITICAL .getProcess());
				achievementInfo.setProcessNode(pNode);
				page = achievementInfoService.findPage(new Page<AchievementInfo>(request, response), achievementInfo); 
			} else {
				pNode = String.valueOf(AchievementEnum.DEPARTMENT.getProcess());
				achievementInfo.setOfficeId(officeId);
				achievementInfo.setProcessNode(pNode);
				page = achievementInfoService.findPage(new Page<AchievementInfo>(request, response), achievementInfo); 
			}	
		}
		model.addAttribute("pNode", pNode);
		model.addAttribute("page", page);
		return "modules/kpi/achievementExamineList";
	}
	
//	@RequiresPermissions("kpi:achievementInfo:del")
	@RequestMapping(value = "calculate")
	@ResponseBody
	public AchievementInfo calculate(ResultVo resultVo, RedirectAttributes redirectAttributes) {
		AchievementInfo achievementInfo = achievementInfoService.findByPublisher(resultVo);
		return achievementInfo;
	}
	@RequestMapping(value = "status")
	public String status(AchievementInfo achievementInfo,RedirectAttributes redirectAttributes ,HttpServletRequest request, HttpServletResponse response, Model model) {
//		result.setUser(UserUtils.getUser());
//		Page<Result> page = resultService.findPage(new Page<Result>(request, response), result); 
//		
		if(StringUtils.isNotBlank(achievementInfo.getYear())) {
			SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyy");
			try {
				achievementInfo.setCreateDate(simpleDateFormat.parse(achievementInfo.getYear()));
			} catch (ParseException e) {
				addMessage(redirectAttributes, "查询失败 ！失败信息："+e.getMessage());
			}
		}
		if(StringUtils.isNotBlank(achievementInfo.getName())) {
			User user = UserUtils.get(achievementInfo.getName());
			model.addAttribute("pubUser", user);
		}
		if(StringUtils.isNotBlank(achievementInfo.getStatus())) {
			achievementInfo.setProcessNode(String.valueOf(AchievementEnum.END.getProcess()));
		}
		Page<AchievementInfo> findPage = achievementInfoService.findPage(new Page<AchievementInfo>(request, response), achievementInfo);
		model.addAttribute("page", findPage);
		model.addAttribute("achievementInfo", achievementInfo);
		return "modules/kpi/achievementInfoStatus";
	}
}